import 'dart:io';
import 'package:appinio_video_player/appinio_video_player.dart';
import 'package:flutter/material.dart';
import 'package:flutter_project/common/utils/screen_size_utils.dart';


class VideoPlayerView extends StatefulWidget {
 final String path;

 const VideoPlayerView({this.path = '', super.key});

  @override
  State<VideoPlayerView> createState() => _VideoPlayerViewState();
}

class _VideoPlayerViewState extends State<VideoPlayerView> {
  late VideoPlayerController _controller;
  late CustomVideoPlayerController _customVideoPlayerController;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: ScreenSizeUtils.deviceScreenWidth(context),
      height: 200,
      child: CustomVideoPlayer(
        customVideoPlayerController: _customVideoPlayerController,
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    String path = widget.path;
    if (path.startsWith('http')) {
      _controller = VideoPlayerController.networkUrl(Uri.parse(path))
        ..initialize().then((_) {
          if (!mounted) return;
          setState(() {});
        });
    } else {
      _controller = VideoPlayerController.file(File(path))
        ..initialize().then((_) {
          if (!mounted) return;
          setState(() {});
        });
    }

    _customVideoPlayerController = CustomVideoPlayerController(
      context: context,
      videoPlayerController: _controller,
      customVideoPlayerSettings: const CustomVideoPlayerSettings(
        showPlayButton: true,
        showFullscreenButton: true,
        settingsButtonAvailable: false,
        autoFadeOutControls: false,
      ),
    );
   // _controller.play();
  }

  @override
  void dispose() async{
    super.dispose();
   await _controller.pause();
    _controller.dispose();

  }
}
